
import json
import requests

predict_url = "http://59.36.121.111:8889/model_server/predict"

pb_data = {
    "model_id": "8492dabe-9b19-11e9-ac1b-0a580610652d",
    "version": "1",
    "data": {
        "instances": [{
            "input": [101, 6821, 702, 3952, 2767, 4696, 4638, 3221, 3766, 3300, 103],
        }]
    }
}

headers = {"STAFFNAME": "changqingai"}
# ret = requests.post(url, timeout=15, json=data, headers=headers)


def http_task_produce(data):
    try:
        ret = requests.post(predict_url, timeout=15, json=data, headers=headers)
        print(ret.text)
        if ret.status_code == 200:
            print("send ok")
        else:
            print("send task failed")
        return json.loads(ret.text)
    except Exception as err:
        print("send task exception: {}".format(err))


def pb_http_request(img_data):
    # pb_data["data"]["instances"][0]["input"] = img_data
    # pb_data = {
    #     "model_id": "8492dabe-9b19-11e9-ac1b-0a580610652d",
    #     "version": "4",
    #     "data": {
    #         "inputs":
    #             {"input_image": img_data}
    #
    #     }
    # }
    pb_data = {
        "model_id": "25040e7c-9be8-11e9-b23b-0a580610652d",
        # "model_id": "8492dabe9b1911e9ac1b0a580610652d",
        "version": "2",
        # "signature_name": "serving_default",
        "data": {"instances": [img_data]}
    }
    # pb_data = {
    #     "model_id": "8492dabe-9b19-11e9-ac1b-0a580610652d",
    #     "version": "4",
    #     "data": {"inputs": img_data}
    # }
    return http_task_produce(pb_data)
